﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Submit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string username = HttpContext.Current.User.Identity.Name.ToString();
        string classname = System.Web.HttpContext.Current.Request.Params.Get("name");
        string type = System.Web.HttpContext.Current.Request.Params.Get("action");
        string replyid = System.Web.HttpContext.Current.Request.Params.Get("replyid");

        //If this page is invalidly accessed.
        if (username == null || username.Trim().Equals("")
            || classname == null || classname.Trim().Equals("")
            || type == null || type.Trim().Equals("")) {
            System.Web.HttpContext.Current.Response.Redirect("~/index.aspx");
        }

        ((TextBox)DetailsView1.FindControl("TextBox1")).Text = username;
        ((TextBox)DetailsView1.FindControl("TextBox2")).Text = classname;
        ((TextBox)DetailsView1.FindControl("TextBox5")).Text = System.DateTime.Now.ToString();
        ((TextBox)DetailsView1.FindControl("TextBox6")).Text = "-1";
        if (replyid != null && !replyid.Equals(""))
            ((TextBox)DetailsView1.FindControl("TextBox6")).Text = replyid;
        HyperLink1.NavigateUrl = "~/Class.aspx?name=" + classname;

        ((TextBox)DetailsView2.FindControl("TextBox1")).Text = username;
        ((TextBox)DetailsView2.FindControl("TextBox2")).Text = classname;
        ((TextBox)DetailsView2.FindControl("TextBox3")).Text = System.DateTime.Now.ToString();

        
        if(type.Equals("eval")){
            DetailsView1.Visible = false;
        }
        else{
            DetailsView2.Visible = false;
        }

        if (!((TextBox)DetailsView1.FindControl("TextBox6")).Text.Equals("-1"))
        {
            int parentPostId = int.Parse(((TextBox)DetailsView1.FindControl("TextBox6")).Text);

            DataSetTableAdapters.InsertPostTableAdapter adapter = new DataSetTableAdapters.InsertPostTableAdapter();
            DataSet.InsertPostDataTable results = adapter.GetData();
            foreach (DataSet.InsertPostRow row in results)
            {
                if (row.Id == parentPostId)
                    ((TextBox)DetailsView1.FindControl("TextBox3")).Text = "RE: " + row.Subject;
            }
        }
    }
    protected void PostButton_Click(object sender, EventArgs e)
    {
        System.Web.HttpContext.Current.Response.Redirect("Class.aspx?name=" + ((TextBox)DetailsView1.FindControl("TextBox2")).Text);
    }
    protected void DetailsView1_PageIndexChanging(object sender, DetailsViewPageEventArgs e)
    {

    }
}
